package de.codecasa.andro.kasse;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;


public class DatabaseManager extends SQLiteOpenHelper {
	 
	  private Context context;
	  
	  DatabaseManager(Context context){
	    super(context, context.getResources().getString(R.string.dbname), null,
	        Integer.parseInt(context.getResources().getString(R.string.version)));
	    this.context=context;
	  }

	  @Override
	  public void onCreate(SQLiteDatabase db) {
	    for(String sql : context.getResources().getStringArray(R.array.create))
	    {
	    	try
	    	{
	    		db.execSQL(sql);
	    	}
	        catch (Exception ex) 
	        {
	        	Toast.makeText(context, ex.toString(), Toast.LENGTH_LONG).show();
	        }    	    	
	    }  
	  }


	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    	try
    	{
    		Log.w(DatabaseManager.class.getName(),
    				"Upgrading database from version " + oldVersion + " to "
    				+ newVersion + ", which will destroy all old data");

				db.execSQL("DROP TABLE IF EXISTS artikel;");
				db.execSQL("DROP TABLE IF EXISTS buchungen;");
				db.execSQL("DROP TABLE IF EXISTS kategorien;");
    			
    			onCreate(db);
    	
    	}
        catch (Exception ex) 
        {
        	Toast.makeText(context, ex.toString(), Toast.LENGTH_LONG).show();
        }    	    	
    	

	}	  
	  
	
	}



